package com.hsteach.util;

import java.util.ArrayList;
import java.util.List;


public class MyPager<T> {
    // 需要传入分页的数据集合
    private List<T> dataList;
    // 当前页号
    private int currentPages;
    // 每页条数
    private int size;
    // 分页后的返回集合
    private List<T> resultsList;
    // 页数
    private int pages;
    // 记录条数
    private int recordCount;
    // 上一页序号
    private int prePageIndex;
    // 下一页序号
    private int nextPageIndex;
    // 是否第一页
    private boolean firstPage;
    // 是否最后一页
    private boolean lastPage;
    //是否有前一页
    private boolean hasPreviousPage = false;
    //是否有下一页
    private boolean hasNextPage = false;
    //所有导航页号
    private int[] navigatepageNums;

    public int getCurrentPages() {
        return currentPages;
    }

    public void setCurrentPages(int currentPages) {
        this.currentPages = currentPages;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
        // 计算条数
        recordCount = dataList.size();
        // 计算页数
        /*if (recordCount % size == 0) {
            pages = recordCount / size;
        } else {
            pages = recordCount / size + 1;
        }*/
        pages=(int)Math.ceil(Double.valueOf(recordCount)/Double.valueOf(size));

        //是否有下一页
        if (currentPages>1){
            hasPreviousPage=true;
        }else {
            hasPreviousPage=false;
        }
        if (currentPages<pages){
            hasNextPage=true;
        }else {
            hasNextPage=false;
        }

        navigatepageNums = new int[pages];
        for (int i = 0; i < pages; i++) {
            navigatepageNums[i] = i + 1;
        }
        // 上一页，下一页确定
        if(currentPages>1){
            prePageIndex = currentPages - 1;

        }
        if (currentPages<pages){
            nextPageIndex = currentPages + 1;
        }
        // 是否第一页，最后一页
        if (currentPages == 1) {
            firstPage = true;
        } else {
            firstPage = false;
        }
        if (currentPages == pages) {
            lastPage = true;
        } else {
            lastPage = false;
        }

        // 筛选工作
        resultsList = new ArrayList<T>();
        for (int i = (currentPages - 1) * size; i < currentPages * size && i < recordCount; i++) {
            resultsList.add(dataList.get(i));
        }

    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public List<T> getResultsList() {
        return resultsList;
    }

    public void setResultsList(ArrayList<T> resultsList) {
        this.resultsList = resultsList;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getRecordCount() {
        return recordCount;
    }

    public void setRecordCount(int recordCount) {
        this.recordCount = recordCount;
    }

    public int getNextPageIndex() {
        return nextPageIndex;
    }

    public void setNextPageIndex(int nextPageIndex) {
        this.nextPageIndex = nextPageIndex;
    }

    public int getPrePageIndex() {
        return prePageIndex;
    }

    public void setPrePageIndex(int prePageIndex) {
        this.prePageIndex = prePageIndex;
    }

    public boolean isFirstPage() {
        return firstPage;
    }

    public void setFirstPage(boolean firstPage) {
        this.firstPage = firstPage;
    }

    public boolean isLastPage() {
        return lastPage;
    }

    public void setLastPage(boolean lastPage) {
        this.lastPage = lastPage;
    }

    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public int[] getNavigatepageNums() {
        return navigatepageNums;
    }

    public void setNavigatepageNums(int[] navigatepageNums) {
        this.navigatepageNums = navigatepageNums;
    }

}
